package com.ceo.auth.jwt;


import com.ceo.auth.jwt.chain.CallableChain;
import com.ceo.auth.jwt.wrapper.JwtWrapper;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 77294 on 2020/9/10.
 * Jwt Token 访问验证拦截器
 */
public class JwtChainIntercepter implements HandlerInterceptor {

    @Resource
    CallableChain<Boolean, JwtWrapper> defaultJwtChain;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        JwtWrapper jwtWrapper = new JwtWrapper();
        if(jwtWrapper.support(handler)){
            jwtWrapper.setRequest(request);
            jwtWrapper.setResponse(response);
            jwtWrapper.setHandler((HandlerMethod)handler);
            try {
                Boolean proceed = defaultJwtChain.doProceed(() -> jwtWrapper);
                return proceed;
            } catch (Exception e) {
                throw e;
            }
        }else{
            return true;
        }
    }

}
